﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DAL;

namespace BLL
{
    public class ForBookAdd
    {
        //查询基本类
        public SqlDataAdapter SeeBookClass()
        {
            //查询基本类
            string bClass = @"select * from [dbo].[BookClass];";
            SqlDataAdapter adapter= new SqlDataAdapter(bClass, DBHelper.Connection);
            return adapter;
        }
        //查询具体类
        public SqlDataAdapter SeeBookType(int ClassID=-10)
        {
            string type;
            if (ClassID != -10)
            {
                //SQL语句
                type = string.Format(@"select [BookTypeID],[BookType] 
                        from [dbo].[BookType] where ClassID = {0}"
                        , ClassID);
            }
            else
            {
                type = "select [BookTypeID],[BookType] from [dbo].[BookType]";
            }
            SqlDataAdapter adapter = new SqlDataAdapter(type, DBHelper.Connection);
            return adapter;
        }

        //添加至Book表
        public int AddToBook(string BookName,string Press,string Author,int Words,float Price,int ClassID,int BookTypeID)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(string.Format(@"insert into Book(BookName,Press,Author,Words,Time,Price,ClassID,BookTypeID)
                        values ('{0}','{1}','{2}','{3}',GETDATE(),'{4}',{5},{6});
                        select @@IDENTITY;", BookName, Press, Author, Words, Price, ClassID, BookTypeID));
            int ID = Convert.ToInt32(DBHelper.ExecuteScalar(sb.ToString()));
            return ID;
        }
        //添加至Books表
        public int AddToBooks(int ID,int stateID)
        {
            StringBuilder sb = new StringBuilder();
            //编写SQL语句，添加图书
            sb.Append(string.Format("insert into Books(ID,StateID) values({0},{1});select @@identity;", ID, stateID));
            string sb2 = string.Format("insert into BookState(StateID,State) values({0},{1});", stateID, stateID);
            DBHelper.ExecuteScalar(sb2.ToString());
            //执行，查询图书编号
            int bookID = Convert.ToInt32(DBHelper.ExecuteScalar(sb.ToString()));
            return bookID;
        }
    }
}
